package com.itboye.hutouben.activity.mysetting;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.presenter.UserPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.util.SPUtils;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

public class SendYzmOneActivity extends BaseActivity implements Observer {
    ImageView close_icon;// 返回图片
    TextView add_shap_title_tv;// 标题
    TextView send_one_tel;
    TextView send_one_ok;

    String mobile;//手机号

    UserPresenter userPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_yzm_one);
        add_shap_title_tv.setText(R.string.zhaohui_pass);
        userPresenter=new UserPresenter(this);

        mobile=(String) SPUtils.get(this,null, Const.MOBILE,"");

        if (!mobile.equals("")) {
            try {
                String hintphone = mobile.substring(0, 3) + "****"
                        + mobile.substring(7, 11);
                send_one_tel.setText(hintphone);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            send_one_tel.setText("");
        }
    }
    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close_icon:
                finish();
                break;
            case R.id.send_one_ok:

                userPresenter.sendVerificationCode("+86",mobile,2+"");

                break;
            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity=handlerError(data);
        if(entity!=null){
            IsUtilUid.tianjia(entity);
            if(entity.getEventType()==UserPresenter.send_code_success){
                ByAlert.alert(entity.getData());
                Intent intent=new Intent(this,SendYzmTwoActivity.class);
                intent.putExtra("phone", send_one_tel.getText().toString());
                intent.putExtra("mobile", mobile);
                startActivity(intent);
            }
            if(entity.getEventType()==UserPresenter.send_code_fail){
                ByAlert.alert(entity.getData());

            }
        }
    }
}
